<?php
namespace app\service;
use think\Db;
class personService
{
	//根据personGuid查询用户信息
	public function personinfo($whe){
		return Db::table("personnel")->where($whe)->find();
	}
	//新增数据并返回主键值
	public function personAdd($data){
		return Db::table("personnel")->insertGetId($data);
	}
	//根据房屋id获取业主信息
	public function ownerinfo($room_id){
		return Db::table("personnel")->where("room_id",$room_id)->where("relation",1)->find();
	}
	public function roominfo($building,$unit,$room)
	{
		return Db::table("room")->where("building",$building)->where("unit",$unit)->where("room",$room)->find();
	}
	//用户添加
	public function useradd($user_id,$name,$phone,$relation)
	{
		//查询该用户已经添加的人数
		$num=Db::table("personnel")
					->where("parent_user_id",$user_id)
					->count();
		if($num>=8){
			return json(["errCode"=>"1",'errMsg' => '最多添加8个用户']);
		}
		//查询该业主一分钟内是否添加用户信息
		$useradd=Db::table("personnel")
					->where("parent_user_id",$user_id)
					->where("create_time",">",(time()-60))
					->find();
		if(!empty($useradd))
		{
			return json(["errCode"=>"1",'errMsg' => '用户已添加']);
		}
		//查询业主信息
		$userinfo=Db::table("personnel")
					->where("user_id",$user_id)
					->where("relation",0)
					->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => '业主信息不存在']);
		}
		$res=Db::table("personnel")
					->where("phone",$phone)
					->find();
		if(!empty($res)){
			return json(["errCode"=>"1",'errMsg' => '该手机号已经存在，不能重复添加']);
		}
		$data["community_id"]=$userinfo["community_id"];
		$data["community_name"]=$userinfo["community_name"];
		$data["xmcode"]=$userinfo["xmcode"];
		$data["group"]=$userinfo["group"];
		$data["building"]=$userinfo["building"];
		$data["unit"]=$userinfo["unit"];
		$data["room"]=$userinfo["room"];
		$data["room_id"]=$userinfo["room_id"];
		$data["address"]=$userinfo["address"];

		$data["relation"]=$relation;
		$data["name"]=$name;
		$data["phone"]=$phone;

		$data["parent_user_id"]=$user_id;

		$data["create_time"]=time();

		$res=Db::table("personnel")->insertGetId($data);
		if($res)
		{
			return json(["errCode"=>"0",'errMsg' => '用户添加成功',"uid"=>$res]);
		}else
		{
			return json(["errCode"=>"1",'errMsg' => '用户添加失败,请重新添加']);
		}
	}


	//用户添加
	public function visitoradd($user_id,$name,$phone)
	{
		//查询该业主一分钟内是否添加用户信息
		$useradd=Db::table("visitor")
					->where("parent_user_id",$user_id)
					->where("create_time",">",(time()-60))
					->find();
		if(!empty($useradd))
		{
			return json(["errCode"=>"1",'errMsg' => '访客添加失败']);
		}
		//查询业主信息
		$userinfo=Db::table("personnel")
					->where("user_id",$user_id)
					->where("relation",1)
					->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => '该业主信息不存在,您无法添加']);
		}
		$data["community_id"]=$userinfo["community_id"];
		$data["community_name"]=$userinfo["community_name"];
		$data["group"]=$userinfo["group"];
		$data["building"]=$userinfo["building"];
		$data["unit"]=$userinfo["unit"];
		$data["room"]=$userinfo["room"];
		$data["room_id"]=$userinfo["room_id"];

		$data["relation"]=$relation;
		$data["name"]=$name;
		$data["phone"]=$phone;
		$data["parent_user_id"]=$user_id;

		$data["create_time"]=time();

		$res=Db::table("personnel")->insert($data);
		if($res)
		{
			return json(["errCode"=>"0",'errMsg' => '用户添加成功']);
		}else
		{
			return json(["errCode"=>"1",'errMsg' => '用户添加失败,请重新添加']);
		}
	}
}
